package com.xiaochong.tir.manager.controller;

import java.util.Date;
import javax.servlet.http.HttpSession;

import com.xiaochong.service.common.config.oss.OSSTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.SyRotationMap;
import com.xiaochong.tir.manager.service.SyRotationMapService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;

/**
 * web 轮番图管理
 * @author 杨松松
 *
 */
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX + "/banner")
@Api("web研报banner管理")
public class SyRotationMapController {
	@Autowired
	private SyRotationMapService syRotationMapService;
	@Autowired
	private OSSTool ossTool;
	/**
	 * 轮番图列表页
	 * @return
	 */
	@RequestMapping("/syRotationMapList")
	public String syRotationMapList() {
		return "/syRotationMap/syRotationMapList";
	}

	/**
	 * 查询轮番图列表数据
	 * @param limit
	 * @param page
	 * @param syRotationMap
	 * @return
	 */
	@ApiOperation(value = "查询web研报banner列表",notes = "查询web研报banner列表")
	@RequestMapping("/listSyRotationMap")
	@ResponseBody
	public Object listSyRotationMap(@RequestParam("limit") int limit, @RequestParam("page") int page,
			SyRotationMap syRotationMap) {
		try {
			PageInfo<SyRotationMap> pageInfo = syRotationMapService.findList(syRotationMap, page, limit);
			for (int i = 0; i < pageInfo.getList().size(); i++) {
				SyRotationMap map = pageInfo.getList().get(i);
				if(StringUtils.isNotBlank(map.getImgUrl())&& !map.getImgUrl().contains("http")){
					map.setImgUrl(ossTool.getOSSUrl(map.getImgUrl()));
				}
			}
			return new ReturnResult<SyRotationMap>(0, "查询成功！", pageInfo.getList(), pageInfo.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/listSyRotationMap", e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 轮番图添加页
	 * @return
	 */
	@RequestMapping("/syRotationMapAdd")
	public String syRotationMapAdd() {
		return "/syRotationMap/syRotationMapAdd";
	}

	/**
	 * 新增轮番图
	 * @param syRotationMap
	 * @return
	 */
	@ApiOperation(value = "新增web研报banner",notes = "新增web研报banner")
	@RequestMapping("/saveSyRotationMap")
	@ResponseBody
	public Object saveSyRotationMap(SyRotationMap syRotationMap) {
		try {
			syRotationMap.setStatus(1);
			syRotationMap.setCreateTime(new Date());
			syRotationMap.setUpdateTime(new Date());
			syRotationMapService.saveSyRotationMap(syRotationMap);
			return new ReturnResult<SyRotationMap>(0, "新增成功！", 1, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/saveSyRotationMap", e.getMessage(), e);
			return new ReturnResult<SyRotationMap>(-1, "新增失败", 0, 0);
		}
	}

	/**
	 * 修改轮番图状态
	 * @param id
	 * @param status
	 * @param msg
	 * @param session
	 * @return
	 */
	@ApiOperation(value = "修改web研报banner状态",notes = "修改web研报banner状态")
	@RequestMapping("/updateStatus")
	@ResponseBody
	public Object updateStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status,
			@RequestParam("msg") String msg, HttpSession session) {
		try {
			SyRotationMap syRotationMap = new SyRotationMap();
			syRotationMap.setId(id);
			syRotationMap.setStatus(status);
			syRotationMap.setUpdateTime(new Date());
			int count = syRotationMapService.updateSyRotationMap(syRotationMap);
			return new ReturnResult<SyRotationMap>(0, msg, count, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/updateEnableFlag", e.getMessage(), e);
			return new ReturnResult<SyRotationMap>(-1, "保存失败", 0, 0);
		}
	}

	/**
	 * 查看轮番图
	 * @param id
	 * @param model
	 * @return
	 */
	@ApiOperation(value = "查看web研报banner",notes = "查看web研报banner")
	@RequestMapping("/syRotationMapView")
	public String syRotationMapView(@RequestParam("id") Integer id, Model model) {
		SyRotationMap syRotationMap = syRotationMapService.getSyRotationMap(id);
		if(StringUtils.isNotBlank(syRotationMap.getImgUrl())&&!syRotationMap.getImgUrl().contains("http")){
			syRotationMap.setImgUrl(ossTool.getOSSUrl(syRotationMap.getImgUrl()));
		}
		model.addAttribute("data", syRotationMap);
		return "/syRotationMap/syRotationMapView";
	}

}
